/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.resourcemanager.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Repository;

import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.system.auth.resourcemanager.model.PlatResource;
import cn.com.ksplatform.modules.system.auth.rolemanager.model.PlatRole;

/**
 * ClassName:PlatResourceService <br/>
 * Date: 2015年5月11日 下午9:12:26 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Repository("system.modules.system.PlatResourceService")
public class PlatResourceService extends BaseService {
	@Transactional
	@CachePut(value="ResourceCache",key="#resource.getResourceid()")
	public PlatResource save(PlatResource resource) {
		String id = resource.save();
		resource.setResourceid(id);
		return resource;
	}

	@Transactional
	@CachePut(value="ResourceCache",key="#resource.getResourceid()")
	public PlatResource update(PlatResource resource) {
		return resource.update();
	}
	
	@Transactional
	@CachePut(value="ResourceCache",key="#resource.getResourceid()")
	public PlatResource updateResourceOnly(PlatResource resource) {
		String hql = "update PlatResource r set r.resourceName = ? , r.link = ? ,r.enabled = ? where r.resourceid = ?";
		int r = hibernateDAO.executeUpdate(hql, resource.getResourceName(),resource.getLink(),resource.getEnabled(),resource.getResourceid());
		return r > 0 ? resource : null;
	}
	
	@Transactional
	@CacheEvict(value="ResourceCache",key="#resource.getResourceid()")
	public PlatResource delete(PlatResource resource) {
		return resource.delete();
	}

	@Transactional
	@CachePut(value="ResourceCache",key="#resource.getResourceid()")
	public PlatResource saveOrUpdate(PlatResource resource) {
		return resource.saveOrUpdate();
	}
	
	@Transactional
	public List<PlatResource> getRess(Set<PlatRole> roles){
		List params = new ArrayList();
		if(roles.size() == 0){
			return null;
		}
		String hql = "";
		for(PlatRole r : roles){
			hql+=("?,");
			params.add(r.getRoleId());
		}
		hql= hql.substring(0,hql.length()-1);
		List<PlatResource> buttons = (List<PlatResource>) super.getHibernateDAO().findAll("select distinct u from PlatResource as u , PlatRole as r where u.resourceid in elements(r.resources) and r.id in ("+hql+") order by u.resourceid",params);
		
		return buttons;
	}
	
	@Transactional
	public List<PlatResource> getAllRess(){
		List<PlatResource> resources = (List<PlatResource>) super.getHibernateDAO().findAll("from PlatResource");
		return resources;
	}
	
	@Transactional
	public List<PlatResource> getRess(Set<PlatRole> roles,String menuId){
		List params = new ArrayList();
		if(roles.size() == 0){
			return null;
		}
		String hql = "";
		for(PlatRole r : roles){
			hql+=("?,");
			params.add(r.getRoleId());
		}
		params.add(menuId);
		hql= hql.substring(0,hql.length()-1);
		List<PlatResource> buttons = (List<PlatResource>) super.getHibernateDAO().findAll("select distinct u from PlatResource as u , PlatRole as r where u.resourceid in elements(r.resources) and r.id in ("+hql+") and u.menu.menuId = ? order by u.resourceid",params);
		
		return buttons;
	}
}
